<template>
  <div class="my-target cardPage">
    <!-- 搜索 -->
    <search :key="levelType" :level-type="levelType" @search="search">
    </search>

    <!-- tabs数据 -->
    <div class="level-content">
      <cardToList :tabs="tabs" :card-data="cardData" :level-type="levelType" :checkStatus="true" :isEdit="true" @jumpList="jumpList" @edit="edit" @tabClick="tabClick" @changeId="changeId" @changePage="changePage"></cardToList>
    </div>

    <!-- 新增、编辑、复制目标标准弹框 -->
    <add-dialog v-if="addParams.visible" :levelType="levelType" :addParams="addParams" :currentId="currentId" :parentData="cardData" @getList="getList"></add-dialog>
  </div>
</template>

<script>
import search from '@/components/search/levelSearch'
import cardToList from '@/components/cardToList/index'
import addDialog from '@/views/targetSystem/targetManage/module/addLevel'
import { getStandardList, delMyTarget } from '../../../api/target'
import { getStorage } from '../../../config/utils'

export default {
  name: 'standardManage',
  components: {
    search,
    cardToList,
    addDialog
  },
  data() {
    return {
      searchFrom: {
        state: 1, // 0-未下发 1-待审核（下发后） 2-审核通过 3-发布成功 4-审核不通过
        name: '',
        dutyPart: '',
        year: '',
        rectificationRangeId: 0,
        pageIndex: 1,
        pageSize: 10
      },
      tabs: [],
      // 层级：学校层，专业层，课程层，教师层，学生层
      levelType: '',
      // 新增相关参数
      addParams: {
        visible: false,
        title: ''
      },
      cardData: [],
      currentId: '' // 当前选的id
    }
  },
  created() {
    // 年度默认为当前年份
    this.searchFrom.year = new Date().getFullYear()
    this.levelType = this.$route.params.type
    this.setTabsByType()
  },
  methods: {
    // 新增、复制、新增子项目标标准弹框
    handleDialog(type) {
      switch (type) {
        case 'add':
          this.addParams = {
            visible: true,
            title: '添加目标标准',
            type: 'add'
          }
          break
        case 'copy':
          this.addParams = {
            visible: true,
            title: '复制目标标准',
            type: 'copy'
          }
          break
        case 'child':
          this.addParams = {
            visible: true,
            title: '新增子项',
            type: 'child'
          }
          break
        case 'set':
          break
      }
    },
    // 根据菜单上的层级查询对应的适用范围
    setTabsByType() {
      const list = getStorage('rangeList')
      if (list && list.length) {
        const temp = list.filter(item => item.code === this.levelType)
        if (temp.length) {
          this.tabs = temp[0].rectificationRangeDTOList

          // 默认显示第一个tabs页的数据
          this.searchFrom.rectificationRangeId = this.tabs[0].id
          this.getList()
        }
      }
    },
    // 获取列表
    getList() {
      getStandardList(this.searchFrom)
        .then(res => {
          this.cardData = res
        })
        .catch()
    },
    // 查询
    search(searchFrom) {
      this.searchFrom.name = searchFrom.name
      this.searchFrom.dutyPart = searchFrom.dutyPart
      this.searchFrom.year = searchFrom.year
      this.getList()
    },
    // 跳转进入列表页
    jumpList(id, data) {
      this.$router.push({
        path: `/target/standardManage/levelList/${this.levelType}`,
        query: {
          index: 2,
          id: id,
          rectificationRangeId: data.rectificationRangeId
        }
      })
    },
    // 编辑
    edit(id) {
      this.$router.push({
        path: `/target/myTarget/levelList/${this.levelType}`,
        query: {
          index: 1,
          id: id
        }
      })
    },
    // 切换tabs
    tabClick(id) {
      this.searchFrom.rectificationRangeId = +id
      this.getList()
    },
    // 选中的卡片
    changeId(id) {
      this.currentId = id
    },
    // 切换分页
    changePage(pageIndex) {
      this.searchFrom.pageIndex = pageIndex
      this.getList()
    }
  },
  watch: {
    $route(to, from) {
      this.levelType = to.params.type
      this.setTabsByType()
    }
  }
}
</script>

<style scoped>
</style>
